﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_User_ChooseUser : System.Web.UI.Page
{
    protected string pDel_mk = "", pUpd_mk = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);
        if (clsAuth.IsDeleteAuth() == true)
            pDel_mk = "Y";
        if (clsAuth.IsUpdateAuth() == true)
            pUpd_mk = "Y";

        if (!IsPostBack)
        {
            if (clsAuth.IsAddAuth() == false)
                btnAdd.Visible = false;

            if (Request.QueryString["Type"] == "NewUser")
            {
                ddlAut.Visible = true;
                BindAuthorityList();
            }
            BinData();
        }
    }

    private void BindAuthorityList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlAut.DataSource = cls.Get_shr_AuthorityNoneProc().Tables[0];
        ddlAut.DataTextField = "AutNm";
        ddlAut.DataValueField = "AutID";
        ddlAut.DataBind();
        item.Text = "-- Chọn quyền hạn --";
        item.Value = "0"; //
        ddlAut.Items.Insert(0, item);



    }

    private void BinData()
    {
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("CusNm", txtName.Text.Trim());
        msg.CreateFirstNode("UserNm", txtLogin.Text.Trim());
        msg.CreateFirstNode("Email", txtEmail.Text.Trim());
        msg.CreateFirstNode("Phone", txtPhone.Text.Trim());
        msg.CreateFirstNode("AutID", ddlAut.SelectedValue);
        msg.CreateFirstNode("UserID", "0");
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        if (Request.QueryString["Type"] == "NewUser")
        {
            ds = cls.Get_shr_UserCondition(msg.GetXmlStr);
        }
        else
        {
            ds = cls.Get_shr_CustomerCondition(msg.GetXmlStr);
        }
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

        if (Session["IsAdmin"] != "S")
        {
            grv_Master.Columns[4].Visible = false;
        }
        if (Request.QueryString["Type"] != "NewUser")
        {
            grv_Master.Columns[10].Visible = false;
            grv_Master.Columns[0].Visible = false;
            grv_Master.Columns[1].Visible = false;
        }

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddUser.aspx?CodeID=" + Request.QueryString["CodeID"]);
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinData();
    }
    protected void Grv_Product_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {


            e.Row.Cells[1].Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");

            /*if (DataBinder.Eval(e.Row.DataItem, "UpdID").ToString() == "-1")
            {

                Label lblUserNm = (Label)e.Row.FindControl("lblUserNm");
                lblUserNm.Text = "Administrator";
            }*/

            //Truong hop xoa
            if (pDel_mk != "Y")
            {
                e.Row.Cells[1].Text = "";

                e.Row.Cells[1].Attributes.Add("onclick", "javascript:return false;");
            }
            //Truong hop Update
            if ( pUpd_mk != "Y")
            {
                e.Row.Cells[0].Text = "";
                // e.Row.Cells[10].Text = "";
                e.Row.Cells[0].Attributes.Add("onclick", "javascript:return false;");
            }

            if (Request.QueryString["Type"] == "NewUser")
            {
                e.Row.Cells[10].Text = DataBinder.Eval(e.Row.DataItem, "AutNm").ToString();
            }

            e.Row.Attributes.CssStyle.Add("cursor", "pointer");
            e.Row.Attributes.Add("onclick", "javascript:window.opener.fnChooseUser('" + DataBinder.Eval(e.Row.DataItem, "UserID") + "'); window.close();");
            e.Row.Attributes.Add("onmouseover", "this.className='cursorOverGrid'");
            if (e.Row.RowIndex % 2 == 0)
                e.Row.Attributes.Add("onmouseout", "this.className='gvRowStyle'");
            else
                e.Row.Attributes.Add("onmouseout", "this.className='gvAlternatingRowStyle'");
        }



    }

    protected void grv_Master_RowCreated1(object sender, GridViewRowEventArgs e)
    {
        /*for (int i = 0; i < grv_Master.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.WebControl myControl = grv_Master.Rows[i].Cells[0];
            myControl.Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
        }*/

    }

    protected void grv_Master_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("UserID", grv_Master.DataKeys[e.RowIndex].Value.ToString());

        cls.Del_shr_User(msg.GetXmlStr.ToString());
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        BinData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }
    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        BinData();
    }
    protected void btnAdd_Click1(object sender, EventArgs e)
    {
        Response.Redirect("AddCustomer.aspx?CodeID=" + Request.QueryString["CodeID"] + "&Type=" + Request.QueryString["Type"] + "");
    }
    protected void ddlAut_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinData();
    }
}
